package com.wondertek.poms.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author gaozhengxing gaozhenghang@wondertek.com.cn
 * @date 2021/1/4 13:46
 * @description TODO
 */
@Slf4j
public class FileUploadUtil {
    public static boolean copyFileFilter(String source, String target) {
        String folderPath = source.substring(0, source.lastIndexOf("/"));
        String fileName = source.substring(source.lastIndexOf("/") + 1);
        String prefixFileName = fileName.substring(0, fileName.lastIndexOf("*"));
        File fullDir = new File(folderPath);
        File[] allFile = fullDir.listFiles(new PagingFileFilter(fileName));
        if (allFile != null && allFile.length > 0) {
            File[] arr$ = allFile;
            int len$ = allFile.length;

            label134:
            for(int i$ = 0; i$ < len$; ++i$) {
                File file = arr$[i$];
                InputStream in = null;
                FileOutputStream out = null;

                try {
                    int bytesum = 0;
                    String fileSuffix = file.getName().substring(prefixFileName.length());
                    String targetFilePath = target.substring(0, target.lastIndexOf(".")) + fileSuffix;
                    newFile(targetFilePath);
                    in = new FileInputStream(file);
                    out = new FileOutputStream(targetFilePath, false);
                    byte[] buffer = new byte[4096];

                    while(true) {
                        if ((bytesum = in.read(buffer)) == -1) {
                            continue label134;
                        }

                        bytesum += bytesum;
                        out.write(buffer, 0, bytesum);
                    }
                } catch (Exception var26) {
                    System.err.println("Exception occur when copying a file");
                    var26.printStackTrace();
                } finally {
                    try {
                        if (in != null) {
                            in.close();
                        }

                        if (out != null) {
                            out.close();
                        }
                    } catch (IOException ignored) {
                        log.error("文件上传失败："+ignored.toString());
                    }

                }
                return false;
            }
        }

        return true;
    }
    public static boolean newFile(String fullName) {
        try {
            File file = new File(fullName);
            if (!file.exists()) {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            }

            return true;
        } catch (Exception var2) {
            System.out.println("error occur when creating folder");
            var2.printStackTrace();
            return false;
        }
    }
    public static boolean delFile(String fullName) {
        try {
            File file = new File(fullName);
            return !file.exists() || file.delete();
        } catch (Exception var2) {
            var2.printStackTrace();
            return false;
        }
    }

    public static boolean moveFile(String oldPath, String newPath) {
        return copyFile(oldPath, newPath) && delFile(oldPath);
    }
    public static boolean copyFile(String source, String target) {
        boolean isSuc = false;
        InputStream in = null;
        FileOutputStream out = null;

        try {
            int bytesum = 0;
            File oldfile = new File(source);
            newFile(target);
            if (oldfile.exists()) {
                in = new FileInputStream(source);
                out = new FileOutputStream(target, false);
                byte[] buffer = new byte[4096];

                int byteread;
                while((byteread = in.read(buffer)) != -1) {
                    bytesum += byteread;
                    out.write(buffer, 0, byteread);
                }
                isSuc = true;
            } else {
                System.err.println("File " + source + " not exists");
            }
        } catch (Exception var21) {
            System.err.println("Exception occur when copying a file");
            var21.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException var20) {
            }

            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException var19) {
            }

        }

        return isSuc;
    }

}
